package com.truyentranh.requester;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.truyentranh.data.AdsDataObject;
import com.truyentranh.data.HaiBLMetalData;
import com.truyentranh.data.HaiBLModule;
import com.truyentranh.network.Request;
import com.truyentranh.network.RequestListener;
import com.truyentranh.network.UrlHelper;
import com.truyentranh.utils.AppSettingParams;
import com.truyentranh.utils.Logger;

public class TruyenTranhRequester extends Request{
	
	private RequestListener requestListener;	
	private String method = "";
	
	private TruyenTranhRequester(RequestListener listener){
		this.requestListener = listener;
	}
	
	public static TruyenTranhRequester getCategory(RequestListener listener){
		TruyenTranhRequester tranhRequester = new TruyenTranhRequester(listener);
		tranhRequester.method = "category";
		tranhRequester.requestListener = listener;
		return tranhRequester;
	}
	
	String id = "";
	int page = 1;
	int count = 50;
	public static TruyenTranhRequester detailCategory(String id, int page, int count, RequestListener listener){
		TruyenTranhRequester tranhRequester = new TruyenTranhRequester(listener);
		tranhRequester.method = "detailCategory";
		tranhRequester.requestListener = listener;
		tranhRequester.id = id;
		tranhRequester.page = page;
		tranhRequester.count = count;
		return tranhRequester;
	}
	
	String href = "";
	public static TruyenTranhRequester getComicInfo(String href, RequestListener listener){
		TruyenTranhRequester tranhRequester = new TruyenTranhRequester(listener);
		tranhRequester.method = "getcommicinfo";
		tranhRequester.requestListener = listener;
		tranhRequester.href = href;
		return tranhRequester;
	}
	
	public static TruyenTranhRequester getComic(String href, RequestListener listener){
		TruyenTranhRequester tranhRequester = new TruyenTranhRequester(listener);
		tranhRequester.method = "getcommic";
		tranhRequester.requestListener = listener;
		tranhRequester.href = href;
		return tranhRequester;
	}
	
	public static TruyenTranhRequester getComicImage(String href, RequestListener listener){
		TruyenTranhRequester tranhRequester = new TruyenTranhRequester(listener);
		tranhRequester.method = "getimagecommic";
		tranhRequester.requestListener = listener;
		tranhRequester.href = href;
		return tranhRequester;
	}
	
	@Override
	public void cancelRequest() {
		// TODO Auto-generated method stub		
	}

	@Override
	public void runRequest() {
		// TODO Auto-generated method stub
		if("category".equals(method)){
			String uri = UrlHelper.BASE_URL_TRUYEN_TRANH + method + "?";
			url = UrlHelper.getUrlOfRequestUpdates(uri);
		} else if ("detailCategory".equals(method)){
			String uri = UrlHelper.BASE_URL_TRUYEN_TRANH + method + "?";
			url = UrlHelper.getUrlOfMethod(uri, 
					new String[] {"id","page","count"},
					new String[] { id , String.valueOf(page), String.valueOf(count)});
		} else if ("getcommicinfo".equals(method) 
				|| "getcommic".equals(method)
				|| "getimagecommic".equals(method)){
			String uri = UrlHelper.BASE_URL_TRUYEN_TRANH + method + "?";
			url = UrlHelper.getUrlOfMethod(uri, 
					new String[] {"href"},
					new String[] { href});
		}
		send();
	}	
	
	JSONObject jsonObject = null;
	public JSONObject getData(){
		return this.jsonObject;
	}
	@Override
	public void requestFinished(JSONObject jsonObject) {
		this.jsonObject = jsonObject;
		if(requestListener != null){
			requestListener.onRequestComplete(this);
		}
	}

	@Override
	public void requestFailed(int errorCode, String message) {
		// TODO Auto-generated method stub
		if(requestListener != null){
			requestListener.onRequestFailed(message, this);
		}
	}
}
